﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace TCCUIControls
{
    public partial class UIArbiter : UserControl
    {
        public delegate void NextDelegate();
        private int m_Padding = 5;
        private Brush m_Brush = new SolidBrush(Color.LightSeaGreen);
        private Brush m_WhiteBrush = new SolidBrush(Color.White);
        private Brush m_TextBrush = new SolidBrush(Color.Black);
        private Pen m_Pen = new Pen(Color.Gray);
        private string m_Name;
        private NextDelegate m_NextDelegateMethod;
        private NextDelegate m_CheckDelegateMethod;

        public UIArbiter(ListView listView, NextDelegate nextMethod, NextDelegate checkMethod)
        {
            m_Name = "Arbiter";
            m_NextDelegateMethod = nextMethod;
            m_CheckDelegateMethod = checkMethod;
            lstProcessors = listView;

            InitializeComponent();

            UIArbiter.m_RTB = rtbConsole;
        }

        private void pnlName_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;

            Rectangle rect = pnlName.ClientRectangle;
            rect.Width--;
            rect.Height--;

            g.FillRectangle(m_WhiteBrush, rect);
            g.DrawRectangle(m_Pen, rect);

            rect.X += m_Padding;
            rect.Y += m_Padding;
            rect.Width -= 2 * m_Padding;
            rect.Height -= 2 * m_Padding;

            g.FillRectangle(m_Brush, rect);
            g.DrawRectangle(m_Pen, rect);

            rect.X += m_Padding;
            rect.Y += m_Padding;
            rect.Width -= 2 * m_Padding;
            rect.Height -= 2 * m_Padding;

            SizeF textSize = g.MeasureString(m_Name, pnlName.Font);
            g.DrawString(m_Name, pnlName.Font, m_TextBrush, rect.X + rect.Width / 2 - textSize.Width / 2, rect.Y);
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            if (m_NextDelegateMethod != null)
                m_NextDelegateMethod.Invoke();
        }

        private void chkManualContol_CheckedChanged(object sender, EventArgs e)
        {
            btnNext.Enabled = chkManualContol.Checked;
            m_CheckDelegateMethod.Invoke();
        }

        private static RichTextBox m_RTB = null;

        private delegate void WriteToConsoleDelegate(string str);
        public static void WriteToConsole(string str)
        {
            if (m_RTB.InvokeRequired)
            {
                m_RTB.BeginInvoke(new WriteToConsoleDelegate(WriteToConsole), str);
                return;
            }

            m_RTB.AppendText(String.Format("> {0}\n", str));
            m_RTB.ScrollToCaret();
        }
    }
}
